﻿using EMoney.IStock.Client.Models.MC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EMoney.IStock.Client.DataAccess.MC
{
    /// <summary>
    /// 任务完成记录数据访问类
    /// </summary>
    public class CompleteTaskRecordDal
    {
        /// <summary>
        /// 新增任务完成记录
        /// </summary>
        /// <param name="record"></param>
        /// <returns></returns>
        public int AddCompleteTaskRecord(MC_CompleteTaskRecord record)
        {
            string sql = "insert into MC_CompleteTaskRecord(UserID,TaskID,iNum,CompleteDate,RecordTime) values(@UserID,@TaskID,@iNum,@CompleteDate,@RecordTime)";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, record.UserID);
            tools.AddSqlParameter("TaskID", System.Data.SqlDbType.Int, record.TaskID);
            tools.AddSqlParameter("iNum", System.Data.SqlDbType.Int, record.iNum);
            tools.AddSqlParameter("CompleteDate", System.Data.SqlDbType.DateTime, record.CompleteDate);
            tools.AddSqlParameter("RecordTime", System.Data.SqlDbType.DateTime, record.RecordTime);

            return SQLHelper.ExecuteNonQuery(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取用户指定日期完成任务记录
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="completeDate"></param>
        /// <returns></returns>
        public List<MC_CompleteTaskRecord> GetCompleteTaskRecord(int userid, DateTime completeDate)
        {
            string sql = "select * from MC_CompleteTaskRecord where UserID=@UserID and CompleteDate=@CompleteDate";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, userid);
            tools.AddSqlParameter("CompleteDate", System.Data.SqlDbType.DateTime, completeDate);

            return SQLHelper.GetDataByColumnsLst<MC_CompleteTaskRecord>(ConnectionManager.Emfstock, sql, tools.GetSqlParameters());
        }

        /// <summary>
        /// 获取用户完成指定任务的数量
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="taskID"></param>
        /// <returns></returns>
        public int GetCompleteTaskRecord(int userid, int taskID)
        {
            string sql = "select count(0) from MC_CompleteTaskRecord where UserID=@UserID and TaskID=@TaskID";

            SQLTools tools = new SQLTools();

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, userid);
            tools.AddSqlParameter("TaskID", System.Data.SqlDbType.Int, taskID);

            return Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionManager.Emfstock, System.Data.CommandType.Text, sql, tools.GetSqlParameters()));
        }

        /// <summary>
        /// 获取任务完成记录
        /// </summary>
        /// <param name="record"></param>
        /// <returns></returns>
        public int GetCompleteTaskRecord(MC_CompleteTaskRecord record)
        {
            SQLTools tools = new SQLTools();
            StringBuilder sql = new StringBuilder();

            sql.Append("select count(0) from MC_CompleteTaskRecord where UserID=@UserID and TaskID=@TaskID and CompleteDate=@CompleteDate");

            tools.AddSqlParameter("UserID", System.Data.SqlDbType.Int, record.UserID);
            tools.AddSqlParameter("TaskID", System.Data.SqlDbType.Int, record.TaskID);
            tools.AddSqlParameter("CompleteDate", System.Data.SqlDbType.DateTime, record.CompleteDate);

            return Convert.ToInt32(SQLHelper.ExecuteScalar(ConnectionManager.Emfstock,System.Data.CommandType.Text,sql.ToString(),tools.GetSqlParameters()));
        }
    }
}
